package com.bjtime.hprinter.Fragments.attrs

import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bjtime.designer.attributes.QRCodeAttr
import com.bjtime.designer.widget.BaseLabelView
import com.bjtime.designer.widget.QRCodeView2
import com.bjtime.hprinter.Dialogs.MIUIInputDialog
import com.bjtime.hprinter.R
import kotlinx.android.synthetic.main.viewpager_attrs_layout_base.*


class qrcode  : Fragment , View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    constructor()

    private var QRView  : QRCodeView2? = null
    private var QRCODE  : RadioButton? = null
    private var MAXCODE : RadioButton? = null

    companion object {
        fun newInstance () : qrcode {
            return qrcode ()
        }
    }

    fun setView (v : BaseLabelView) {
        QRView = v as QRCodeView2
        if (QRView != null) {

            QRText!!.setText(QRView!!.des_text)
            QRCODE!!.setOnCheckedChangeListener(null)
            MAXCODE!!.setOnCheckedChangeListener(null)
            if (QRView!!.des_type == QRCodeAttr.QRTYPE.MAXI_CODE) {
                QRCODE!!.isChecked = false
                MAXCODE!!.isChecked = true
            } else {
                QRCODE!!.isChecked = true
                MAXCODE!!.isChecked = false
            }
            QRCODE!!.setOnCheckedChangeListener(this)
            MAXCODE!!.setOnCheckedChangeListener(this)
        }
    }


    private var mView : View? = null
    private var QRText : TextView? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = LayoutInflater.from(context).inflate(R.layout.viewpager_attrs_layout_qrcode, null)
        mView!!.findViewById<CheckBox>(R.id.attrs_base_expand).setOnCheckedChangeListener({ i, b->
            if (b) {
                mView!!.findViewById<View>(R.id.attrs_base_layout).visibility = View.VISIBLE
                text1.setText(resources.getString(R.string.bastattr_left))
                (text1.parent as LinearLayout).setBackgroundColor(Color.TRANSPARENT)
            } else {
                mView!!.findViewById<View>(R.id.attrs_base_layout).visibility = View.GONE
                text1.setText(resources.getString(R.string.bastattr_expand))
                (text1.parent as LinearLayout).setBackgroundColor(Color.WHITE)
            }
        })
        QRCODE = mView!!.findViewById(R.id.qr_type_qrcode)
        MAXCODE = mView!!.findViewById(R.id.qr_type_max)

        QRCODE!!.setOnCheckedChangeListener(this)
        MAXCODE!!.setOnCheckedChangeListener(this)

        QRText = mView!!.findViewById(R.id.qr_tv_settext)
        if (QRView != null)
            QRText!!.setText(QRView!!.des_text)
        QRText!!.setOnClickListener(this)
        return mView
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        if (isChecked) {
            if (QRView != null) {
                if (buttonView == QRCODE) {
                    QRView!!.setType(QRCodeAttr.QRTYPE.QR_CODE)
                } else {
                    QRView!!.setType( QRCodeAttr.QRTYPE.MAXI_CODE)
                }
            }
        }
    }

    override fun onClick(v: View?) {
        var dialog  = MIUIInputDialog(context!!)
        dialog.setTitle("二维码内容")
        dialog.setMaxLenght(128)
        dialog.setString (QRText!!.text.toString())
        dialog.show()
        dialog.setOnButtonClickListener(object : MIUIInputDialog.OnButtonClick {
            override fun onOk(v: View) {
                QRText!!.setText(dialog.getString ())
                if (QRView != null)
                    QRView!!.setText(QRText!!.text.toString())
            }

            override fun onCancel(v: View) {

            }
        })
    }
}